<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/8/3/003
 * Time: 15:49
 */

namespace App\Http\Controllers\Category;

use App\Http\Controllers\Controller;
use App\Http\Controllers\PublicController;

use Illuminate\Http\Request;
use App\Common\ErrorDefine;
use Crypt;
use App\Model\Fg_cate;
use App\Model\Fg_keyword;
class CateController extends Controller
{

    private $public;
    private $Cate;
    private $Keyword;
    private $page = 0;
    private $limit = 10;

    public function __construct(PublicController $public)
    {
        $this->Cate = new Fg_cate();
        $this->Keyword = new Fg_keyword();
        $this->public = $public;
    }

    /**
     * @param Request $request
     * 添加分类
     */
    public function addCate(Request $request)
    {

        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }


        //  分类名
        if(empty($request->input('cate_name')) || !$request->has('cate_name'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        $params['cate_name'] = $request->input('cate_name');

        $Cate = $this->public->checkCateExist($params['cate_name']);

        if($Cate != NULL)
        {
            self::ShowError(ErrorDefine::CATE_EXIST);
            return;
        }


        try{

            $this->Cate->insert($params);

            return self::ajaxReturn(0,'success');

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

    }


    /**
     * @param Request $request
     * 修改分类
     */
    public function updateCate(Request $request)
    {
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        //  分类id
        if(empty($request->input('cate_id')) || !$request->has('cate_id'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //  分类名
        if(empty($request->input('cate_name')) || !$request->has('cate_name'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        $cate_id = $request->input('cate_id');

        $cate = $this->Cate->where('id',$cate_id)->first();

        if($cate == NULL)
        {
            self::ShowError(ErrorDefine::CATE_NOT_EXIST);
            return;
        }

        try{
            $cate->updated_at = date('Y-m-d H:i:s');
            $cate->cate_name = $request->input('cate_name');
            $cate->save();

            return self::ajaxReturn(0,'success');

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

    }

    /**
     * @param Request $request
     * 分类信息列表
     */
    public function getCateList(Request $request)
    {

        if(!$request->has('token') && empty($request->input('token'))){
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        $catelist = $this->Cate->get()->toArray();
        $count = $this->Cate->count();

        return self::ajaxReturn(0,'success',$catelist,$count);

    }


    /**
     * @param Request $request
     * 添加关键词
     */
    public function addKeyword(Request $request)
    {

        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        if(!$request->has('token') && empty($request->input('token'))){
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }



        //  关键词
        if(empty($request->input('keyword')) || !$request->has('keyword'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //  所属分类id
        if(empty($request->input('cate_id')) || !$request->has('cate_id'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        $params['keyword'] = $request->input('keyword');
        $params['cate_id'] = $request->input('cate_id');
        $Keyword = $this->public->checkKeyExist($params['keyword']);

        if($Keyword != NULL)
        {
            self::ShowError(ErrorDefine::KEYWORD_EXIST);
            return;
        }

        try{

            $this->Keyword->insert($params);

            return self::ajaxReturn(0,'success');

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }


    }


    /**
     * @param Request $request
     * 修改关键词
     */
    public function updateKeyword(Request $request)
    {
        if(empty($request->all()))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }


        if(!$request->has('token') || empty($request->input('token')))
        {
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        //  关键词id
        if(empty($request->input('key_id')) || !$request->has('key_id'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //  分类id
        if(empty($request->input('cate_id')) || !$request->has('cate_id'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        //  分类名
        if(empty($request->input('keyword')) || !$request->has('keyword'))
        {
            self::ShowError(ErrorDefine::INVALID_PARAMS);
            return;
        }

        $key_id = $request->input('key_id');

        $Keyword = $this->Keyword->where('id',$key_id)->first();

        if($Keyword == NULL)
        {
            self::ShowError(ErrorDefine::KEYWORD_NOT_EXIST);
            return;
        }

        try{
            $Keyword->updated_at = date('Y-m-d H:i:s');
            $Keyword->cate_id = $request->input('cate_id');
            $Keyword->keyword = $request->input('keyword');
            $Keyword->save();

            return self::ajaxReturn(0,'success');

        }catch (\Exception $exception){

            self::ShowError(ErrorDefine::DATABASE_ERROR);
            return;
        }

    }

    /**
     * @param Request $request
     * 关键词列表
     */
    public function getKeywordList(Request $request)
    {

        if(!$request->has('token') && empty($request->input('token'))){
            self::showError(ErrorDefine::ACCOUNT_LOGIN_EXPIRED);
            return;
        }

        if($request->input('page'))
        {
            $this->page  = $request->input('page');
            if($this->page){
                $this->page = $this->page - 1;
                if(is_numeric($this->page)){
                    if($this->page<0){
                        $this->page = 0;
                    }
                }
            }
        }

        if($request->input('limit'))
        {
            $this->limit = $request->input('limit');
            if($this->limit){
                if($this->limit <=0 ){
                    $this->limit =  10;
                }
            }
        }

        if($request->input('limit') || $request->input('page')){
            $keyword = $this->Keyword->leftJoin('fg_cate','fg_keyword.cate_id','=','fg_cate.id')
                ->offset($this->limit*$this->page)->limit($this->limit)->orderBy('id','asc')
                ->get(['fg_keyword.id','fg_keyword.cate_id','fg_keyword.keyword','fg_keyword.created_at','fg_cate.cate_name'])
                ->toArray();
        }else{
            $keyword = $this->Keyword->leftJoin('fg_cate','fg_keyword.cate_id','=','fg_cate.id')
                ->where('cate_id',1)->orderBy('id','asc')
                ->get(['fg_keyword.id','fg_keyword.cate_id','fg_keyword.keyword','fg_keyword.created_at','fg_cate.cate_name'])
                ->toArray();
        }





        $count = $this->Keyword->count();

        return self::ajaxReturn(0,'success',$keyword,$count);
    }

}
